package xin.sunzy.spring.service.impl;

import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xin.sunzy.spring.dao.LearnMapper;
import xin.sunzy.spring.model.LearnResource;
import xin.sunzy.spring.service.LearnService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * description: [描述文本][optional]
 * email: <a href="sunzhenyu@terminus.io">孙振宇</a>
 * author: ginkgo
 * date: 2018/3/11
 */
@Slf4j
@Service
public class LearnServiceImpl implements LearnService {

    @Autowired
    private LearnMapper learnMapper;

    @Override
    public List<LearnResource> queryLearnResourceList(Map<String, Object> params) throws Exception {
        try {
            PageHelper.startPage(Integer.parseInt(params.get("page").toString()), Integer.parseInt(params.get("rows").toString()));
            List<LearnResource> learnResources = this.learnMapper.queryLearnResourceList(params);
            Map<String, List<LearnResource>> collect = learnResources.stream().collect(Collectors.groupingBy(LearnResource::getAuthor));
            System.out.println(collect);
            return learnResources;
        } catch (Exception e) {
            log.error("query.learn.resource.failed,cause:{}", e.getMessage());
            throw new Exception("query.learn.resource.failed");
        }
    }
}
